队列的实现（Queue.h）#pragma once#include<stdio.h>#include<assert.h>#include<stdlib.h>typedef int TYPE;typedef struct QlistNode {        struct QlistNode* next;        TYPE date;}QNode;typedef struct Queue {        QNode* front;//队头        QNode* fear;//队尾}Queue;extern QNode* BuyNode(TYPE date);//节点的创建extern void QueueInit(Queue* q);//初始化队列extern void QueuePush(Queue* q, TYPE data);//队尾入队列extern void QueuePop(Queue* q);//队头出队列extern int QueueEmpty(Queue* q);//检测队列是否为空extern TYPE QueueFront(Queue* q);//获取队列头部元素extern TYPE QueueFear(Queue* q);//获取队列尾部元素extern int QueueSize(Queue* q);//获取队列中有效元素个数extern void QueueDestroy(Queue* q); //销毁队列
队列的实现（Queue.c）
#include"stack.h"
//节点的创建
QNode* BuyNode(TYPE date){
        QNode* new_node = (QNode*)malloc(sizeof(QNode));
        if (!new_node)
               assert(0);
        new_node->next = NULL;
        new_node->date = date;
        return new_node;
}
//初始化
void QueueInit(Queue* q) {
        QNode* head = BuyNode(0);
        q->front = head;
        q->fear = head;
        return;
}
//队尾入队列
void QueuePush(Queue* q, TYPE data) {
        assert(q);
        QNode* new_node = BuyNode(data);
        q->fear->next = new_node;
        q->fear = new_node;
        return;
}
//队头出队列
void QueuePop(Queue* q) {
        if (QueueEmpty(q))//检测是否为空
               return;
        QNode* del_node = q->front->next;
        q->front->next = del_node->next;
        //注意当只有一个有效元素时，将队尾指针指向头，否则将成为野指针
        if (del_node == q->fear)
               q->fear = q->front;
        free(del_node);
        return;
}
//检测队列是否为空
int QueueEmpty(Queue* q) {
        assert(q);
        return q->front == q->fear;
}
//获取队列头部元素
TYPE QueueFront(Queue* q) {
        if (QueueEmpty(q)) {
               printf("队列为空!\n");
               return -1;
        }
        return q->front->next->date;
}
//获取队列尾部元素
TYPE QueueFear(Queue* q) {
        if (QueueEmpty(q)){
               printf("队列为空!\n");
               return -1;
        }
        return q->fear->date;
}
//获取队列中有限元素个数
int QueueSize(Queue* q) {
        assert(q);
        int count = 0;
        QNode* cur = q->front->next;
        while (cur) {
               cur = cur->next;
               count++;
        }
        return count;
}
//销毁队列
void QueueDestroy(Queue* q) {
        assert(q);
        QNode* del_node = q->front->next;
        while (del_node) {
               q->front->next = del_node->next;
               free(del_node);
               del_node = q->front->next;
        }
        free(q->front);
        q->front = q->fear = NULL;
}
//测试
void Test() {
        Queue q;//申请一个队列结构体变量，在此处栈上申请同在初始化函数里在堆上申请效果相同，此处申请不需要free
        QueueInit(&q);
        QueuePush(&q, 1);
        QueuePush(&q, 2);
        QueuePush(&q, 3);
        printf("size = %d\n", QueueSize(&q));
        printf("front = %d\n", QueueFront(&q));
        printf("fear = %d\n", QueueFear(&q));
        QueuePop(&q);
        QueuePop(&q);
        QueuePop(&q);
        printf("size = %d\n", QueueSize(&q));
        printf("front = %d\n", QueueFront(&q));
        printf("fear = %d\n", QueueFear(&q));
        
        //QueueFront(&q)
        QueueDestroy(&q);
        return;
}


